<?php

/**
 * @Author: Wang Chunsheng 2192138785@qq.com
 * @Date:   2020-03-04 00:17:41
 * @Last Modified by:   Wang Chunsheng 2192138785@qq.com
 * @Last Modified time: 2020-03-16 06:36:50
 */


namespace app\modules\diandi_shop\controllers\order;

use api\modules\diandi_shop\services\OrderService;
use Yii;
use app\modules\diandi_shop\models\DdOrder;
use app\modules\diandi_shop\models\DdOrderAddress;
use app\modules\diandi_shop\models\DdOrderGoods;
use app\modules\diandi_shop\models\enums\OrderStatus;
use app\modules\diandi_shop\models\searchs\DdOrderSearch;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backend\controllers\BaseController;
use common\helpers\ErrorsHelper;
use common\helpers\FileHelper;
use common\helpers\ResultHelper;
use common\models\DdCorePaylog;
use moxuandi\phpSpreadsheet\ExportData;
use moxuandi\phpSpreadsheet\ExportModel;

/**
 * DdOrderController implements the CRUD actions for DdOrder model.
 */
class DdOrderController extends BaseController
{



    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }



    /**
     * Lists all DdOrder models.
     * @return mixed
     */
    public function actionIndex()
    {
        // $data = [
        //     ['id' => 1, 'name' => '张三', 'sex' => '男', 'phone' => 15515181929, 'idc' => 211325659536289526],
        //     ['id' => 2, 'name' => '李四', 'sex' => '男'],
        //     ['id' => 5, 'name' => '王二', 'sex' => '女'],
        //     ['id' => 8, 'name' => '麻子', 'sex' => '女'],
        // ];
       
        
        $searchModel = new DdOrderSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        
        // ExportModel::widget([
        //     'models' => DdOrder::find()->all(),  // 必须
        //     'asAttachment' => true,  // 默认值, 可忽略
        // ]);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single DdOrder model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        $detail = OrderService::detail($id);
        // print_r($this->findModel($id));
        return $this->render('view', [
            'model' => $this->findModel($id),
            'detail'=>$detail
        ]);
    }

    /**
     * Creates a new DdOrder model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new DdOrder();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->order_id]);
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing DdOrder model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->order_id]);
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    /**
     * Deletes an existing DdOrder model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    // 订单操作
    public function actionConfirm(){
        if(Yii::$app->request->isPost){
            $data = Yii::$app->request->post();
            $ctype = $data['ctype'];
            $order_id= $data['order_id'];
            return OrderService::confirmOrder($order_id,$ctype);
        }
    }

    
    public function actionExportdata()
    {
        ExportModel::widget([
            'models' => DdOrder::find()->all(),  // 必须
            'asAttachment' => true,  // 默认值, 可忽略
        ]);
    }


    public function actionDeletes()
    {
        if(Yii::$app->request->isPost){
            $ids = Yii::$app->request->post('ids');
            $DdOrder = new DdOrder();
            $DdOrderGoods = new DdOrderGoods();
            $DdOrderAddress = new DdOrderAddress();
            
            $DdOrder::deleteAll(['order_id'=>$ids]);
            $DdOrderGoods::deleteAll(['order_id'=>$ids]);
            $DdOrderAddress::deleteAll(['order_id'=>$ids]);
            
            if(ErrorsHelper::getModelError($DdOrder)){
                $errors = ErrorsHelper::getModelError($DdOrder);
                return ResultHelper::json(401,'删除失败',$errors); 
            }
            if(ErrorsHelper::getModelError($DdOrderGoods)){
                $errors =ErrorsHelper::getModelError(DdOrderGoods::className());
                return ResultHelper::json(401,'删除失败',$errors); 

            }
            if(ErrorsHelper::getModelError($DdOrderAddress)){
                $errors =ErrorsHelper::getModelError($DdOrderAddress);
                return ResultHelper::json(401,'删除失败',$errors); 
                
            }
            return ResultHelper::json(200,'删除成功',[]); 
        }  
    }

    /**
     * Finds the DdOrder model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return DdOrder the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = DdOrder::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
